package com.librarydemo.demo.controller;

import com.librarydemo.demo.entity.Book;
import com.librarydemo.demo.entity.BorrowRecord;
import com.librarydemo.demo.entity.Catalog;
import com.librarydemo.demo.entity.User;
import com.librarydemo.demo.service.BookService;
import com.librarydemo.demo.service.UserService;
import com.librarydemo.demo.util.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    BookService bookService;

    @CrossOrigin
    @RequestMapping("/register")
    @ResponseBody
    public Map<String, Boolean> register(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("password2") String password2, @RequestParam("phoneNumber") int phoneNumber){
        User user = new User(1, username, password, 5, phoneNumber, "0.png") ;
        boolean r = userService.register(user);
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", r);
        return map;
    }

    @RequestMapping("/login")
    @ResponseBody
    public User loginCheck(@RequestParam("username") String username, @RequestParam("password") String password){
        return userService.loginCheck(username, password) ;
    }

    @RequestMapping("/nameCheck")
    @ResponseBody
    public Map<String, Boolean> nameCheck(@RequestParam("username") String username){
        boolean r = userService.isNameExist(username);
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", r);
        return map;
    }

    @RequestMapping("/information")
    @ResponseBody
    public User getById(@RequestParam("uid") int uid){
        return userService.getById(uid);
    }


    @RequestMapping("/borrowBook")
    @ResponseBody
    public Map<String, Boolean> borrowBook(@RequestParam("uid") int uid, @RequestParam("bid") int bid) {
        boolean r = userService.borrowBook(uid, bid);
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", r);
        return map;
    }

    @RequestMapping("/rePwd")
    @ResponseBody
    public Map<String, Boolean> rePassword(@RequestParam("uid") int uid, @RequestParam("password") String password){
        boolean r = userService.rePassword(uid, password);
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", r);
        return map;
    }

    @RequestMapping("/allBooks")
    @ResponseBody
    public List<Book> getAllBooks(){
        return bookService.getAllBooks();
    }

    @RequestMapping("/borrowRecord")
    @ResponseBody
    public List<BorrowRecord> getAllBorrowRecord(int uid){
        return userService.getAllBorrowRecord(uid);
    }

    @RequestMapping("/catalog")
    @ResponseBody
    public List<Catalog> getCatalog(){
        return bookService.getCatalog();
    }

    @RequestMapping("/returnBook")
    @ResponseBody
    public Map<String, Boolean> returnBook(@RequestParam("uid") int uid, @RequestParam("bid") int bid){
        boolean r = userService.returnBook(uid, bid);
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", r);

        return map;
    }

    @RequestMapping("/updateImg")
    @ResponseBody
    public Map<String, Boolean> fileUp(@RequestParam("photo")MultipartFile file, @RequestParam("uid") int uid, HttpServletRequest request){
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", false);
        String fname = file.getOriginalFilename();
        System.out.println(fname);

        //获取上传文件的扩展名
        String exname = fname.substring(fname.lastIndexOf("."));
        //构建文件新名字：上传日期 + “_”+用户id
        String newFileName = new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date()) + "_" + uid + exname;

        String savePath = "F://idea-workspace//library123//src//main//resources";

        File f = new File(savePath);
        if(! f.exists() ){
            f.mkdirs();
        }
        if(!file.isEmpty()){
            try{
                file.transferTo(new File(savePath + newFileName));
                if(userService.UpdatePic(uid, newFileName)){
                    map.put("valid", true);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return map;

    }

    @RequestMapping("/getImg")
    @ResponseBody
    public String getImg(int uid){
        String path = "../";
        return path + userService.getPicture(uid);
    }

    @RequestMapping("/getBooks")
    @ResponseBody
    public List<Book> getBooks(String name, String author, String publisher, String catalog){
        return userService.getBooks(name, author, publisher, catalog);
    }
}
